//
//  SDHSportsFollowVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/11.
//

#import "SDHSportsFollowVC.h"
#import "DWSegmentedControl.h"
#import "SDHSportsNotchScreenUtil.h"
#import "SDHSportsTeamInfoCell.h"
#import "SDHSportsMatchInfoCell.h"
#import "SDHSportsHomeNewsCell.h"
#import "SDHSportsDataManager.h"
#import "SDHSportsMatchModel.h"
#import "SDHSportsTeamInfoModel.h"
#import "SDHSportsNewsModal.h"
#import "SDHSportsBaseCell.h"
#import "SDHSportsNewsDetailVC.h"
#import "SDHSportsMatchLiveVC.h"

@interface SDHSportsFollowVC () <DWSegmentedControlDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *teamTable;
@property (nonatomic, strong) UITableView *newsTable;
@property (nonatomic, strong) UITableView *matchTable;

@property (nonatomic, strong) NSMutableArray *teamArray;
@property (nonatomic, strong) NSMutableArray *newsArray;
@property (nonatomic, strong) NSMutableArray *matchArray;

@end

@implementation SDHSportsFollowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        
    self.view.backgroundColor = UIColorFromRGB(60, 60, 60);
    self.navigationController.navigationBar.hidden = NO;
    
    NSString *title = @"Follow";
    if (_viewType == TypeViewBrowse) {
        title = @"Browse";
    }
    self.title = title;
    
    [self initUI];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}

- (void)loadData {
    if (TypeViewFollow == _viewType) {
        self.teamArray = [[SDHSportsDataManager sharedManager] getFollowTeam];
        self.newsArray = [[SDHSportsDataManager sharedManager] getFollowNews];
        self.matchArray = [[SDHSportsDataManager sharedManager] getFollowMatch];
    } else {
        self.teamArray = [[SDHSportsDataManager sharedManager] getBrowseTeam];
        self.newsArray = [[SDHSportsDataManager sharedManager] getBrowseNews];
        self.matchArray = [[SDHSportsDataManager sharedManager] getBrowseMatch];
    }
}

- (void)initUI {
    CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    CGFloat navHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat notchHeight = [SDHSportsNotchScreenUtil getIPhoneNotchScreenHeight];
    notchHeight = 0;
    CGFloat posY =  statusBarHeight + navHeight + notchHeight + 10;
    
    DWSegmentedControl *segment = [[DWSegmentedControl alloc] initWithFrame:CGRectMake(20, posY, self.view.frame.size.width-40, 44)];
    segment.backgroundColor = [UIColor whiteColor];
    segment.selectedViewColor = [UIColor grayColor];
    segment.normalLabelColor = [UIColor blackColor];
    segment.delegate = self;
    segment.titles = @[@"Team",@"News",@"Match"];
    [self.view addSubview:segment];
    
    UITableView *table = [UITableView new];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    table.backgroundColor = [UIColor clearColor];
    self.teamTable = table;
    
    table = [UITableView new];
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = [UIColor clearColor];
    [self.view addSubview:table];
    self.newsTable = table;
    
    table = [UITableView new];
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = [UIColor clearColor];
    [self.view addSubview:table];
    self.matchTable = table;
    
    self.teamTable.hidden = NO;
    self.newsTable.hidden = YES;
    self.matchTable.hidden = YES;
    
    self.teamTable.tableFooterView = [UIView new];
    self.newsTable.tableFooterView = [UIView new];
    self.matchTable.tableFooterView = [UIView new];
    
    [self.teamTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.equalTo(segment.mas_bottom);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];

    [self.matchTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.equalTo(segment.mas_bottom);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];

    [self.newsTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.equalTo(segment.mas_bottom);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark -
- (void)dw_segmentedControl:(DWSegmentedControl *)control didSeletRow:(NSInteger)row {
    if (row == 0) {
        self.teamTable.hidden = NO;
        self.matchTable.hidden = YES;
        self.newsTable.hidden = YES;
    } else if (1 == row) {
        self.teamTable.hidden = YES;
        self.newsTable.hidden = NO;
        self.matchTable.hidden = YES;
    } else {
        self.teamTable.hidden = YES;
        self.newsTable.hidden = YES;
        self.matchTable.hidden = NO;
    }
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = 0;
    
    if (tableView == self.teamTable) {
        count = self.teamArray.count;
    } else if (tableView == self.newsTable) {
        count = self.newsArray.count;
    } else {
        count = self.matchArray.count;
    }
    
    if (count == 0) {
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    } else {
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    CGFloat height = 30;
    if (tableView == self.teamTable) {
        height = 100;
    } else if (tableView == self.matchTable) {
        height = 120;
    } else if (tableView == self.newsTable) {
        height = 90;
    }
    
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        if (tableView == self.teamTable) {
            cell = [[SDHSportsTeamInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        } else if (tableView == self.matchTable) {
            cell = [[SDHSportsMatchInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        } else {
            cell = [[SDHSportsHomeNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
    }
    NSObject *model;
    if (tableView == self.teamTable) {
        model = [self.teamArray objectAtIndex:indexPath.row];
    } else if (tableView == self.newsTable) {
        model = [self.newsArray objectAtIndex:indexPath.row];
    } else {
        model = [self.matchArray objectAtIndex:indexPath.row];
    }
    [cell updateContent:model];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSObject *model;
    if (tableView == self.teamTable) {
        model = [self.teamArray objectAtIndex:indexPath.row];
        
        
    } else if (tableView == self.newsTable) {
        model = [self.newsArray objectAtIndex:indexPath.row];
        
        SDHSportsNewsDetailVC *vc = [[SDHSportsNewsDetailVC alloc] init];
        vc.model = (SDHSportsNewsModal *)model;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        model = [self.matchArray objectAtIndex:indexPath.row];
        
        SDHSportsMatchLiveVC *vc = [[SDHSportsMatchLiveVC alloc] init];
        vc.model = (SDHSportsMatchSoccerModel *)model;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
